package com.carboy.launch.widgets;

import android.app.Dialog;
import android.content.Context;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.carboy.launch.R;
import com.carboy.launch.able.AbstractDialogOperate;

/**
 * wifi连接操作Dialog
 */
public class WifiConnectOperateDialog extends Dialog {

	private Context mContext;
	private TextView dwc_ssid_tv; // wifi_ssid
	private Button dwc_cancel_btn; // 取消
	private Button dwc_connect_btn; // 连接

	private LinearLayout dwc_open_linear; // 开放的wifi
	private TextView dwc_sintensity_tv; // 信号强度
	private TextView dwc_safety_tv; // 安全

	private LinearLayout dwc_input_linear; // 有密码的wifi
	private EditText dwc_pwd_edit; // 密码输入框
	private CheckBox dwc_display_checkbox; // 显示密码

	private boolean isOpen; // 是否开放
	private AbstractDialogOperate absDialogOperate; // Dialog操作抽象类

	public WifiConnectOperateDialog(Context context) {
		super(context, R.style.Theme_Light_FullScreenDialogOperate);
		// 默认不显示输入栏，但点击输入框时会弹出来
		getWindow().setSoftInputMode(
				WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
		// 绑定Layout
		this.setContentView(R.layout.dialog_wifi_connect);
		mContext = context;
		// 初始化控件
		dwc_ssid_tv = (TextView) this.findViewById(R.id.dwc_ssid_tv);
		dwc_cancel_btn = (Button) this.findViewById(R.id.dwc_cancel_btn);
		dwc_connect_btn = (Button) this.findViewById(R.id.dwc_connect_btn);

		dwc_open_linear = (LinearLayout) this
				.findViewById(R.id.dwc_open_linear);
		dwc_sintensity_tv = (TextView) this
				.findViewById(R.id.dwc_sintensity_tv);
		dwc_safety_tv = (TextView) this.findViewById(R.id.dwc_safety_tv);

		dwc_input_linear = (LinearLayout) this
				.findViewById(R.id.dwc_input_linear);
		dwc_pwd_edit = (EditText) this.findViewById(R.id.dwc_pwd_edit);
		dwc_display_checkbox = (CheckBox) this
				.findViewById(R.id.dwc_display_checkbox);

		// 禁止返回键关闭(模拟可以返回键关闭Dialog，然后立即点击其他页面，看是否会报错----请求数据异步操作，赋值的时候报null(页面已Stop或者销毁了)----测试)
		// this.setCancelable(false);

		// 设置大小
		// WindowManager.LayoutParams layoutParams =
		// this.getWindow().getAttributes();
		// layoutParams.width = LayoutParams.FILL_PARENT;
		// layoutParams.height = LayoutParams.WRAP_CONTENT;
		// this.getWindow().setAttributes(layoutParams);
	}

	/**
	 * 设置透明度(针对Dialog)
	 */
	public void setAlpha(float alpha) {
		Window window = WifiConnectOperateDialog.this.getWindow();
		WindowManager.LayoutParams lp = window.getAttributes();
		lp.alpha = alpha;
		window.setAttributes(lp);
	}

	/**
	 * 设置背景透明度(针对Dialog背景)
	 */
	public void setDimAmount(float dimAmount) {
		Window window = WifiConnectOperateDialog.this.getWindow();
		WindowManager.LayoutParams lp = window.getAttributes();
		lp.dimAmount = dimAmount;
		window.setAttributes(lp);
		// 背景模糊
		// window.addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
	}

	/**
	 * 设置操作抽象类触发
	 */
	public void setAbsDialogOperate(AbstractDialogOperate adOperate) {
		this.absDialogOperate = adOperate;
		// 设置点击事件,以及触发操作
		dwc_cancel_btn.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				absDialogOperate.executeWifiConnectOperate(false, false, null);
			}
		});
		dwc_connect_btn.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				if (isOpen) {
					absDialogOperate
							.executeWifiConnectOperate(true, true, null);
				} else {
					String pwd = dwc_pwd_edit.getText().toString();
					if (pwd.length() < 8) {
						CustomToast.showToast(mContext, R.string.wifi_pwd_hint);
						return;
					}
					absDialogOperate
							.executeWifiConnectOperate(true, false, pwd);
				}
			}
		});
		dwc_display_checkbox
				.setOnCheckedChangeListener(new OnCheckedChangeListener() {
					@Override
					public void onCheckedChanged(CompoundButton buttonView,
							boolean isChecked) {
						if (isChecked) { // 显示密码
							dwc_pwd_edit
									.setTransformationMethod(HideReturnsTransformationMethod
											.getInstance());
						} else { // 隐藏密码
							dwc_pwd_edit
									.setTransformationMethod(PasswordTransformationMethod
											.getInstance());
						}
						dwc_pwd_edit.postInvalidate();
					}
				});
	}

	/**
	 * 显示dialog
	 */
	public void showDialog(String ssid, boolean isOpen, String strength) {
		this.isOpen = isOpen;
		dwc_ssid_tv.setText(ssid); // 显示wifi名(ssid)
		if (isOpen) { // 开放无密码
			dwc_open_linear.setVisibility(View.VISIBLE);
			dwc_input_linear.setVisibility(View.GONE);

			dwc_sintensity_tv.setText(strength);
			dwc_safety_tv.setText("无");
		} else { // 需要输入密码
			dwc_open_linear.setVisibility(View.GONE);
			dwc_input_linear.setVisibility(View.VISIBLE);
			dwc_display_checkbox.setChecked(false);
			dwc_pwd_edit.setText(null);
		}
		WifiConnectOperateDialog.this.show(); // 弹出dialog
	}

	/**
	 * 关闭Dialog
	 */
	public void cancelDialog() {
		if (this.isShowing()) {
			this.cancel();
		}
	}
}
